using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace SolarWinds.Widgets
{
    public class Spectrum : Widget
    {
        private Texture2D mTexture;

        public double[] SpectrumData;
        public int SpetrumDrawRange = 256;
        public Color Color = Color.White;
        public int FrequencyMark = -1;

        private readonly Rectangle mSrcRect = new Rectangle(16, 32, 16, 16);

        public Spectrum(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
        }

        protected internal override void PaintEvent()
        {
            base.PaintEvent();

            DrawSpectrum();
        }

        private void DrawSpectrum()
        {
            if (SpectrumData == null)
            {
                return;
            }

            var drawRange = (int)MathHelper.Min(SpetrumDrawRange, SpectrumData.Length);

            // The upper band of frequencies at 44khz is pretty boring 
            // (ie 11-22khz), so we are only going to display the first 
            // 256 frequencies, or (0-11khz) 

            double max = 0;
            for (int i = 0; i < drawRange; ++i)
            {
                if (max < SpectrumData[i])
                {
                    max = SpectrumData[i];
                }
            }

            if (FrequencyMark != -1 &&
                FrequencyMark >= 0 &&
                FrequencyMark < drawRange)
            {
                DrawLine(FrequencyMark, Color.Yellow);
            }

            for (int i = 0; i < drawRange; i++)
            {
                DrawSpectrumLine(i, max, Color);
            }
        }

        private void DrawSpectrumLine(int i, double max, Color col)
        {
            double y = SpectrumData[i] / max * Height;

            if (y >= Height)
            {
                y = Height - 1;
            }

            if (y < 0)
            {
                y = 0;
            }

            y = Height - y;

            var destRect = new Rectangle(ScreenPosition.X + i, ScreenPosition.Y + (int)y,
                                         1, (int)(Height - y));

            SpriteBatch.Draw(mTexture, destRect, mSrcRect, col);
        }

        private void DrawLine(int i, Color col)
        {
            var destRect = new Rectangle(ScreenPosition.X + i, ScreenPosition.Y + 0,
                                         1, Height);

            SpriteBatch.Draw(mTexture, destRect, mSrcRect, col);
        }


        protected override void LoadContent()
        {
            base.LoadContent();

            mTexture = Game.Content.Load<Texture2D>(@"Textures\tiles");
        }

        protected override void UnloadContent()
        {
            base.UnloadContent();

            if (mTexture != null)
            {
                mTexture.Dispose();
                mTexture = null;
            }
        }
    }
}